<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\user\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;

/**
 * Class AdminIndexController
 * @package app\user\controller
 *
 * @adminMenuRoot(
 *     'name'   =>'用户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10,
 *     'icon'   =>'group',
 *     'remark' =>'用户管理'
 * )
 *
 * @adminMenuRoot(
 *     'name'   =>'用户组',
 *     'action' =>'default1',
 *     'parent' =>'user/AdminIndex/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>'用户组'
 * )
 */
class AdminIndexController extends AdminBaseController
{

    /**
     * 后台本站用户列表
     * @adminMenu(
     *     'name'   => '本站用户',
     *     'parent' => 'default1',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $content = hook_one('user_admin_index_view');

        if (!empty($content)) {
            return $content;
        }

        $where   = [];
        $request = input('request.');

        if (!empty($request['uid'])) {
            $where['id'] = intval($request['uid']);
        }

        if (!empty($request['keyword'])) {
            $keyword = $request['keyword'];

            $where['user_login|user_nickname|user_email|mobile']    = ['like', "%$keyword%"];
        }

        // 搜索more
        if (!empty($request['company'])) {
            $company = $request['company'];
            $where['more'] = ['like', "%\"company\":\"%$company%\"%"];
        }

        if (!empty($request['subsidiary'])) {
            $subsidiary = $request['subsidiary'];
            $where['more'] = ['like', "%\"subsidiary\":\"%$subsidiary%\"%"];
        }

        $usersQuery = Db::name('user');

        $ban = input('ban', false);
        if ($ban) {
          $usersQuery->where('user_status', 0);
        } else {
          $usersQuery->where('user_status', 1);
        }

        $list = $usersQuery->where($where)->order("create_time DESC")->paginate(10, false, ['query' => request()->param()]);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    public function auditing () {
      $list = Db::name('user')->where('user_status', 2)->order("create_time DESC")->paginate(20);
      $page = $list->render();
      $this->assign('list', $list);
      $this->assign('page', $page);
      // 渲染模板输出
      return $this->fetch();
    }

    public function info_auditing () {
      $list = DB::name('user_auditing')->paginate(20);
      $page = $list->render();
      $this->assign('list', $list);
      $this->assign('page', $page);
      return $this->fetch();
    }

    public function info_auditing_pass() {
      $param = $this->request->param();
      $id = $param['id'];
      $find = DB::name('user_auditing')->where('id', $id)->find();
      if (empty($find)) {
        $this->error('用户不存在');
      }
      $user_id = $find['user_id'];
      $more = json_encode([
        'company' => $find['company'],
        'subsidiary' => $find['subsidiary'],
      ]);
      $ok = DB::name('user')->where('id', $user_id)->update([
        'user_nickname' => $find['user_nickname'],
        'mobile' => $find['mobile'],
        'more' => $more,
      ]);

      if ($ok) {
        DB::name('user_auditing')->delete($id);
        $this->success('审核成功');
      }

    }

    /**
     * 本站用户拉黑
     * @adminMenu(
     *     'name'   => '本站用户拉黑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户拉黑',
     *     'param'  => ''
     * )
     */
    public function ban()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $result = Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 0);
            if ($result) {
                $this->success("会员拉黑成功！", "adminIndex/index");
            } else {
                $this->error('会员拉黑失败,会员不存在,或者是管理员！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    /**
     * 本站用户启用
     * @adminMenu(
     *     'name'   => '本站用户启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户启用',
     *     'param'  => ''
     * )
     */
    public function cancelBan()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 1);
            $this->success("会员启用成功！", '');
        } else {
            $this->error('数据传入失败！');
        }
    }

    public function pass() {
      $id = input('param.id', 0, 'intval');
      if ($id) {
          $result = Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 1);
          if ($result) {
              $this->success("审核成功！");
          } else {
              $this->error('会员审核失败,会员不存在,或者是管理员！');
          }
      } else {
          $this->error('数据传入失败！');
      }
    }

    public function remove() {
      $id = input('param.id', 0, 'intval');
      if ($id) {
          $result = Db::name("user")->delete($id);
          if ($result) {
              $this->success("删除成功！", "adminIndex/index");
          } else {
              $this->error('删除失败！');
          }
      } else {
          $this->error('数据传入失败！');
      }
    }

    public function edit() {
      $id = input('param.id', 0, 'intval');
      if ($id) {
        $info = DB::name('user')->where('id', $id)->find();
        $info['more'] = json_decode($info['more'], true);

        $this->assign('info', $info);
        return $this->fetch();
      } else {
        $this->error('数据传入失败！');
      }
    }

    public function edit_post() {
      $param = $this->request->param();
      if (!isset($param['id'])) {
        $this->error('数据传入失败！');
      }
      $id = $param['id'];

      $validate = new Validate([
          'company' => 'require',
          'subsidiary' => 'require',
          'user_nickname' => 'require',
          'mobile' => 'require',
      ]);
      $validate->message([
          'company.require' => '公司名称不能为空！',
          'subsidiary.require' => '店铺名称不能为空！',
          'user_nickname.require' => '姓名不能为空！',
          'mobile.require' => '手机号码不能为空！',
      ]);

      $data = $this->request->param();
      if (!$validate->check($data)) {
          $this->error($validate->getError());
      }

      $more = [
        'company' => $param['company'],
        'subsidiary' => $param['subsidiary'],
      ];

      $id = $param['id'];
      $ok = DB::name('user')->where('id', $id)->update([
        'user_nickname' => $param['user_nickname'],
        'mobile' => $param['mobile'],
        'more' => json_encode($more),
      ]);

      if ($ok) {
        $this->success('修改成功');
      } else {
        $this->error('修改失败');
      }
    }
}
